#!/bin/bash

ffvar()
{
    if [ -z "$1" ]; then
        ffecho bad "wrong argv"
    fi
}

ffecho()
{
    echo ""
    case $1 in
        bad)
            echo -e "\e[37;41;1m $2 \e[0m" && exit 1;;
        good)
            echo -e "\e[37;42;1m $2 \e[0m";;
        notice)
            echo -e "\e[37;46;1m $2 \e[0m";;
        begin)
            echo -e "\e[37;44;1m $2 \e[0m";;
        *)
            echo -e "\e[37;41;1m wrong argv \e[0m";;
    esac
    echo ""
    sleep 2
}


ffrun()
{
    ffecho begin "we are hardly to $2" 
    $1
    if [ $? = 0 ]; then
        ffecho good "success to $2"
    else
        ffecho bad "Oops,we have problem to $2,Please check log"
        exit 1
    fi
}


ffbackup()
{
    ffvar $1
    for x in $1
    do
      if [ -f $x ]; then
        cp $x $fcbackup
      elif [ -d $x ]; then
        cp -R $x $fcbackup
      else
        ffecho bad "Unsupported type of backup file" 
      fi
    done
}


ffrm()
{
    for x in $1
        do
            if [ -e $x ]; then
                if [ -f $x ]; then
                    rm $x
                elif [ -d $x ]; then
                    rm -rf $x
                else
                    ffecho bad "Bad type of delete file" 
                fi
            fi
        done
}


ffisroot()
{
    if [ $(id -u) != "0" ]; then
        ffecho bad "Warning: You must be root to run this script, please use root to install"
    fi
}


ffinstall()
{
    which $1 > /dev/null
    result=$(echo $?)
    if [ "$result" != "0" ]; then
        ffecho notice "We will begin to install $1"
        apt-get install $1 -y
    fi
}


ffdialog()
{
    ffinstall dialog
    dialog --cr-wrap --msgbox "$1" 10 60
}


ffread()
{
    ffvar $1
    read $1
}

fftrap()
{
    trap $1 $2
}
